const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    notes: '',
    msg:'',
    show:true,
    disabled:false,
    add_member:false,
    book_type:0,
    picker:['普通账本','共享账本'],
    member_list: [],
    member_code:'',
    check_msg:'核验'
  },

  onShow:function(){
    let _this = this;
    _this.getUserInfo(); //获取账本
  },


  check_invi_code:function(){
    let _this = this,
      member_code = _this.data.member_code,
      member_list = _this.data.member_list;
      if(member_code == ''){
        wx.showToast({
          title: '输入框不能为空',
          icon: 'none',
          duration: 2000
        })
      }else{
        _this.setData({
          check_msg:'核验中'
        })
        app._post('api/member/check_invi_code', {member_code:member_code}, function(result) {
          if(result.data!=''){
            let data = new Object();
            data['member_id'] = result.data.member_id;
            data['nickName'] = result.data.nickName;
            //过滤数组，如果member_id已添加，则提示不存在
            var have_index = 0; //验证是否存在，只要有一个相等则条件不成立
            if(member_list.length>0){
              for(var i=0;i<member_list.length;i++){
                if(result.data.member_id == member_list[i].member_id){
                  have_index ++;
                }
              }
            }
            if(have_index == 0){
              member_list.push(data);
            }
            _this.setData({
              member_list:member_list,
              check_msg:'核验'
            })
          }else{
            wx.showToast({
              title: '查无会员，请核实',
              icon: 'none',
              duration: 2000
            })
          }
        });
      }
  },


  input_code:function(e){
    let _this = this,
      input_code = e.detail.value;
    _this.setData({
      member_code:input_code
    })
  },

  del_this:function(e){
    let _this = this,
     del_index = e.currentTarget.dataset.index,
     member_list = _this.data.member_list;
    member_list.splice(del_index,1);
    _this.setData({
      member_list:member_list
    })
  },

  //获取个人信息:确认是否有登陆
  getUserInfo:function(){
    let _this = this;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code != 200){
        wx.navigateTo({
          url: '../auth/auth'
        })
      }
    });
  },

  textareaAInput(e) {
    this.setData({
      notes: e.detail.value
    })
  },

  /*表单验证*/
  validation: function(values) {
    let _this = this;
    if (values.book_name == '') {
      _this.data.msg = '账本名称不能为空';
      return false;
    }
  },

  PickerChange:function(e){
    let _this = this;
    _this.setData({
      book_type:e.detail.value
    })
  },

  /*表单提交*/
  formSubmit: function(e) {
    let _this = this,
      values = e.detail.value,
      book_type = _this.data.book_type;

    // 表单验证
    if (!_this.validation(values)){
      wx.showToast({
        title: _this.data.msg,
        icon: 'none',
        duration: 1500
      })
      return false;
    }

    _this.setData({disabled:true}); //禁止重复点击提交按钮

    return false;
    app._post('api/member/createUserBook',
      {
        book_name:values.book_name,
        notes:_this.data.notes,
      }, 
    function(result) {
      if(result.code == 200){
        app.showSuccess(result.msg);
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000) 
      }
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
})